
import _ from 'lodash';
let StorageKey = 'FasComponentVersionCache'

function getFasComponentVersionCache() {
    let data = localStorage.getItem(StorageKey) || '{}';
    if (data) {
        data = JSON.parse(data);
    } else {
        localStorage.setItem(StorageKey, JSON.stringify(data));
    }
    return data;
}

export function GetComponentVersion(componentType) {
    debugger;
    let data = getFasComponentVersionCache()
    let curVersion = _.get(data, `${componentType}.CurVersion`, "0");
    return +curVersion;
}

export function UpdateComponentVersion(componentType, version) {
    let data = getFasComponentVersionCache()
    _.set(data, `${componentType}.CurVersion`, version)
    localStorage.setItem(StorageKey, JSON.stringify(data));
}

export function SelectHelperVersionChangeHandler(selectHelper) {
    let mixinsComponentVersionCache = WarpMixinsComponentVersionCache("select-helper-warp", selectHelper.Version);
    mixinsComponentVersionCache.UpdateCacheHistory((oldVersion, newVersion, widget) => {
        console.log('FasComponentVersionCache', 'UpdateCacheHistory', 'SelectHelperVersionChangeHandler');
        let state = selectHelper.VersionChangeHandler(oldVersion, newVersion, widget);
        if (state) {
            mixinsComponentVersionCache.FasComponentVersionCache = newVersion;
        }
    })
}

export function WarpMixinsComponentVersionCache(tag, runTimeVersion) {
    let warp = {
        componentVersion: { Tag: tag },
        RunTimeVersion: runTimeVersion,
        ...MixinsComponentVersionCache.methods
    }
    Object.defineProperty(warp, `FasComponentVersionCache`, {
        get() {
            let tag = ((this.componentVersion || this.widget) || this.element).Tag
            return GetComponentVersion(tag)
        },
        set(val) {
            let tag = ((this.componentVersion || this.widget) || this.element).Tag
            UpdateComponentVersion(tag, val)
        }
    });
    return warp;
}

export let MixinsComponentVersionCache = {
    methods: {
        CheckRunTimeVersion() {
            let returnValue = false;
            if (this.RunTimeVersion) {
                if (this.FasComponentVersionCache < this.RunTimeVersion) {
                    returnValue = true;
                }
            }
            return returnValue;
        },
        UpdateCacheHistoryCB(oldVersion, newVersion, widget) {
            return false;
        },
        UpdateCacheHistory(callback) {
            let returnValue = false;
            if (this.CheckRunTimeVersion()) {
                let oldVersion = this.FasComponentVersionCache;
                let newVersion = this.RunTimeVersion;
                let returnValue = (callback || this.UpdateCacheHistoryCB)(oldVersion, newVersion, this)
                if (returnValue) {
                    this.FasComponentVersionCache = this.RunTimeVersion
                }
            }
            return returnValue;
        }
    },
    computed: {
        FasComponentVersionCache: {
            cache: false, 
            get() {
                let tag = ((this.componentVersion || this.widget) || this.element).Tag
                return GetComponentVersion(tag)
            },
            set(version) {
                let tag = ((this.componentVersion || this.widget) || this.element).Tag
                UpdateComponentVersion(tag, version)
            }
        }
    }
}